﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cinser.Common;
using System.Xml;

namespace Cinser.DataTranslate.SettingManager
{
    public static class  SettingsManager
    {
        public static List<ImportSettings> GetImportSettings()
        {
            List<ImportSettings> items = new List<ImportSettings>();
            string xmlPath = GetImportSettingsPath();
            Cinser.Common.XmlHelper xml = new Cinser.Common.XmlHelper(xmlPath);
            string defautSettingId = xml.FirstNode.GetAttributeValue("Default");
            foreach (XmlNode item in xml.FirstNode.ChildNodes)
            {
                ImportSettings s = new ImportSettings();
                s.Id = item.GetAttributeValue("Id");
                s.SettingName = item.GetAttributeValue("SettingName");

                XmlNode sdeNode = item.FindNode("Sde",false)[0];
                s.Sde.Ip = sdeNode.GetAttributeValue("Ip");
                s.Sde.Name = sdeNode.GetAttributeValue("Name");
                s.Sde.Password = sdeNode.GetAttributeValue("Password");
                s.Sde.ServerName = sdeNode.GetAttributeValue("ServerName");
                s.Sde.UserName = sdeNode.GetAttributeValue("UserName");
                s.Sde.Version = sdeNode.GetAttributeValue("Version");

                XmlNode ftpNode = item.FindNode("Ftp", false)[0];
                s.Ftp.Ip = ftpNode.GetAttributeValue("Ip");
                s.Ftp.Password = ftpNode.GetAttributeValue("Password");
                s.Ftp.Port = int.Parse(ftpNode.GetAttributeValue("Port"));
                s.Ftp.UserName = ftpNode.GetAttributeValue("UserName");

                XmlNode sourceNode = item.FindNode("Source", false)[0];
                s.SourceDataPath = sourceNode.GetAttributeValue("Path");
                s.SourceDbPassword = sourceNode.GetAttributeValue("Password");
                if (s.Id == defautSettingId)
                    items.Insert(0, s);
                else
                    items.Add(s);
            }

            return items;
        }

        public static List<TaskSetting> GetTasks()
        {
            List<TaskSetting> items = new List<TaskSetting>();
            string xmlPath = GetTaskSavePath();
            Cinser.Common.XmlHelper xml = new Cinser.Common.XmlHelper(xmlPath);
            foreach (XmlNode item in xml.FirstNode.ChildNodes)
            {
                TaskSetting s = new TaskSetting();
                s.Id = item.GetAttributeValue("Id");
                s.SettingName = item.GetAttributeValue("TaskName");
                s.LogId = item.GetAttributeValue("LogId");
                s.SettingId = item.GetAttributeValue("SettingId");
                s.BeginDate = DateTime.Parse(item.GetAttributeValue("BeginDate"));
                s.FinishDate = DateTime.Parse(item.GetAttributeValue("FinishDate"));
                s.TotalTaskCount = int.Parse(item.GetAttributeValue("TotalTaskCount"));
                s.ErrorNum = int.Parse(item.GetAttributeValue("ErrorNum"));
                s.FinishPercent = double.Parse(item.GetAttributeValue("FinishPercent"));
                items.Add(s);
            }

            return items;
        }

        public static string GetImportSettingsPath()
        {
            return GetSettingPath("ImportSettings.xml");
        }

        public static string GetTaskSavePath()
        {
            return GetSettingPath("Tasks.xml");
        }

        public static string GetSettingPath(string settingFileName)
        {
            //return string.Format("{0}\\App_Data\\{1}", AppUtility.DebugPath, settingFileName);
            return null;
        }
    }
}
